<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/9/12
 * Time: 下午3:00
 */
class CommonService extends Service{

    /**
     * @param $result
     * @param $params
     */
    public static function parseGrid($result){
        $return = array(
            'header'    => array(),
            'data'      => array()
        );

        if($result['state']){
            $return['data'] = $result['data'];

            foreach($result['desc'] as $k=>$v){
                $return['header'][$k] = array('text' => $v);
            }
        }

        return $return;
    }

    /**
     * 将多行Textarea输入的多行的值,转换成用,分隔的字符串,如果内容包含,符号,则替换成 %2C
     * @param $data
     * @return string
     */
    public static function getStringFromMultiLine($data){
        $dataArr = explode("\r\n", $data);
        foreach($dataArr as &$v){
            $v = str_replace(',', '%2C', trim($v));
        }
        unset($v);
        
        $accessory = array();
        foreach ($dataArr as $key=>$val){
        	$accessory['item'][] = array(
        			'id'    => $val,
        	);
        }
        $json_data = json_encode($accessory);
        return $json_data;
        //return implode(',', $dataArr);
    }
}